#!/bin/bash
# 快速测试脚本 - 只测试关键功能

set -e

TEST_DIR="pcie_test_output"
LST_FILE="/home/apachee/trunk/Crystal/CrystalFlowAnalyzer/a9soc/design/pcie/src/DWC_pcie_dm.lst"

echo "========================================="
echo " PCIe 设计快速测试"
echo "========================================="
echo ""

# 测试1: 解析文件列表
echo "[1] 解析文件列表..."
if [ ! -f "$TEST_DIR/pcie_dm.modb" ]; then
    ../mo parse -f $LST_FILE -o $TEST_DIR/pcie_dm.modb > /dev/null 2>&1
fi
echo "✓ 文件列表解析完成"

# 测试2: 模块统计
echo "[2] 模块统计..."
total_modules=$(../mo show -i $TEST_DIR/pcie_dm.modb 2>/dev/null | grep -c "Module name:")
echo "✓ 找到 $total_modules 个模块"

# 测试3: DWC_pcie_dm 实例化检查
echo "[3] DWC_pcie_dm 实例化检查..."
inst_info=$(../mo show -i $TEST_DIR/pcie_dm.modb -m DWC_pcie_dm 2>/dev/null | grep "Instance count:")
echo "  $inst_info"

inst_count=$(echo $inst_info | grep -oE '[0-9]+$')
if [ "$inst_count" -gt 0 ]; then
    echo "✓ 实例化解析成功"
else
    echo "✗ 实例化解析失败"
    exit 1
fi

# 测试4: 关键模块检查
echo "[4] 关键模块检查..."
../mo show -i $TEST_DIR/pcie_dm.modb -m DWC_pcie_dm -v 2>/dev/null > $TEST_DIR/detail.log
if grep -q "pm_ctrl u_pm_ctrl" $TEST_DIR/detail.log && \
   grep -q "cx_pl u_cx_pl" $TEST_DIR/detail.log && \
   grep -q "xadm u_xadm" $TEST_DIR/detail.log; then
    echo "✓ 关键模块都已识别"
else
    echo "⚠ 部分关键模块可能未识别"
fi

# 测试5: 层次结构
echo "[5] 层次结构显示..."
../mo hierarchy -i $TEST_DIR/pcie_dm.modb -m DWC_pcie_dm -l 2 > $TEST_DIR/hierarchy.log 2>&1
if grep -q "Module Hierarchy:" $TEST_DIR/hierarchy.log; then
    echo "✓ 层次结构显示正常"
else
    echo "✗ 层次结构显示失败"
fi

# 测试6: CSV导出
echo "[6] CSV导出..."
../mo hierarchy -i $TEST_DIR/pcie_dm.modb -m DWC_pcie_dm -l 0 -o $TEST_DIR/hierarchy.csv > /dev/null 2>&1
if [ -f "$TEST_DIR/hierarchy.csv" ]; then
    csv_lines=$(wc -l < $TEST_DIR/hierarchy.csv)
    echo "✓ CSV导出成功 ($csv_lines 行)"
else
    echo "✗ CSV导出失败"
fi

echo ""
echo "========================================="
echo " 测试完成"
echo "========================================="
echo "详细日志: $TEST_DIR/"
